package com.edgclub.edg.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.edgclub.edg.R;
import com.edgclub.edg.activity.ChatActivity;
import com.edgclub.edg.activity.GzTopicDetailActivity;
import com.edgclub.edg.activity.GzTopicMoreReplyActivity;
import com.edgclub.edg.activity.GzTopicReplyTopicActivity;
import com.edgclub.edg.activity.UserInfoOtherActivity;
import com.edgclub.edg.adapter.adapteruitls.GzBaseAdapter;
import com.edgclub.edg.adapter.adapteruitls.MyItemClickListener;
import com.edgclub.edg.adapter.adapteruitls.MyItemLongClickListener;
import com.edgclub.edg.adapter.adapteruitls.RViewHolder;
import com.edgclub.edg.model.PostsAlbum;
import com.edgclub.edg.model.Reply;
import com.edgclub.edg.model.Topic;
import com.edgclub.edg.model.User;
import com.edgclub.edg.model.result.RunData;
import com.edgclub.edg.model.result.TopicMessageData;
import com.edgclub.edg.utils.GlideUtil.GlideUtils;
import com.hyphenate.easeui.model.ChatBean;
import com.hyphenate.easeui.utils.DateUtils;

import java.util.Date;
import java.util.List;

/**
 */
public class MessReplyAdapter extends GzBaseAdapter{
    private List<TopicMessageData.TopicMess> datas = null;
    private Context context;
    private GlideUtils glideUtils=new GlideUtils();

    public MessReplyAdapter(Context context) {
        this.context = context;
    }

    //创建新View，被LayoutManager所调用
    @Override
    public MessReplyAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_message_reply,viewGroup,false);
        ViewHolder vh = new ViewHolder(view,mListener,mLongClickListener);
        return vh;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        ViewHolder viewHolder= (ViewHolder) holder;
        final TopicMessageData.TopicMess topicMess=datas.get(position);
         User user=topicMess.getUser();
        if(null==user){
            user=new User();
        }
        final ChatBean chatBean = RunData.getChatBeanMode(null, user);
        viewHolder.mName.setText(user.getNick());
        glideUtils.Glidde_loadCropCircleImage(context,user.getLogo(),viewHolder.mUser_icon);
        final User finalUser1 = topicMess.getUser();
        viewHolder.mUser_icon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(context, UserInfoOtherActivity.class);
                intent.putExtra("user", finalUser1);
                context.startActivity(intent);
            }
        });
        if(!TextUtils.isEmpty(topicMess.getContent())){
            viewHolder.mReply_connten.setText(topicMess.getContent());
        }else{
            viewHolder.mReply_connten.setText("[图片]");
        }

        if(null!=topicMess.getToReply()){
            List<PostsAlbum>albumList=topicMess.getToReply().getAlbums();
            if(null!=albumList&&albumList.size()>0){
                String url=albumList.get(0).getUrl()+"";
                if(url.contains("gz://audio")){
                    viewHolder.mReply_yw.setText("[语音]  "+topicMess.getToReply().getContent());
                }else if(url.contains("video://")){
                    viewHolder.mReply_yw.setText("[视频]  "+topicMess.getToReply().getContent());
                }else{
                    viewHolder.mReply_yw.setText("[图片]  "+topicMess.getToReply().getContent());
                }
            }else{
                viewHolder.mReply_yw.setText(topicMess.getToReply().getContent());
            }
        }
        if("0".equals(topicMess.getTypes())) {
            viewHolder.mReply_type.setText("评论了我的帖子");
        }else if("1".equals(topicMess.getTypes())){
            viewHolder.mReply_type.setText("回复了我的评论");
        }
        viewHolder.mPull_text.setText(DateUtils.getTimestampString(new Date(topicMess.getUpdateAt())));
        viewHolder.mMess_nosee.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                    Intent intent = new Intent(context, ChatActivity.class);
                    intent.putExtra("chatBean", chatBean);
                    context.startActivity(intent);
            }
        });
        final User finalUser = user;
        viewHolder.mReply_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(context,
                        GzTopicReplyTopicActivity.class);
                Reply reply=new Reply();
                reply.setUser(finalUser);
                reply.setId(Long.valueOf(topicMess.getId()));
                intent.putExtra("clickreply", reply);
                intent.putExtra("isReplyComment",true);
                intent.putExtra("topicId", topicMess.getPostsId());
                intent.putExtra("show",true);
                context.startActivity(intent);
            }
        });
        viewHolder.details_layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if ("0".equals(topicMess.getTypes())) {
                    Intent intent = new Intent(context,
                            GzTopicDetailActivity.class);
                    intent.putExtra("topicId", topicMess.getPostsId());
                    context.startActivity(intent);
                }if ("1".equals(topicMess.getTypes())){
                    Intent intent = new Intent(context,
                            GzTopicMoreReplyActivity.class);
                    Topic topic = new Topic();
                    topic.setPostsId(topicMess.getPostsId());
                    topic.setUser(topicMess.getUser());
                    intent.putExtra("topic", topic);
                    try {
                        User user = topicMess.getUser();
                        Reply reply = topicMess.getToReply();
                        reply.setId(topicMess.getMasterReplyId());
                        user.setId(topicMess.getPostsUserId());
                        reply.setUser(user);
                        intent.putExtra("replyfloor", reply);
                        intent.putExtra("yuantie",true);
                        intent.putExtra("load",true);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                    context.startActivity(intent);
                }
            }
        });
        if(null!=user.getUserAuth()){

            if ("-1".equals(user.getUserAuth().getType())){
                viewHolder.isvip.setVisibility(View.GONE);
            }
            if("0".equals(user.getUserAuth().getType())){
                viewHolder.isvip.setVisibility(View.VISIBLE);
                viewHolder.isvip.setImageResource(R.drawable.v_huan);
            }else if("1".equals(user.getUserAuth().getType())){
                viewHolder.isvip.setVisibility(View.VISIBLE);
                viewHolder.isvip.setImageResource(R.drawable.v_lan);
            }
        }
    }


    //获取数据的数量
    @Override
    public int getItemCount() {
//        return 5;
        return datas.size();
    }

    public List<TopicMessageData.TopicMess> getDatas() {
        return datas;
    }

    public void setDatas(List<TopicMessageData.TopicMess> datas) {
        this.datas = datas;
    }




    //自定义的ViewHolder，持有每个Item的的所有界面元素
    public static class ViewHolder extends RViewHolder {
        private ImageView mUser_icon ,isvip;
        private TextView mName;
        private TextView mReply_type;
        private com.rockerhieu.emojicon.EmojiconTextView1 mReply_connten;
        private com.rockerhieu.emojicon.EmojiconTextView1 mReply_yw;
        private TextView mPull_text;
        private TextView mMess_nosee;
        private TextView mReply_btn;
        private RelativeLayout details_layout;
        public ViewHolder(View view, MyItemClickListener listener, MyItemLongClickListener longClickListener){
            super(view, listener, longClickListener);
            mUser_icon = (ImageView) view.findViewById(R.id.user_icon);
            mName = (TextView)  view.findViewById(R.id.name);
            mReply_type = (TextView)  view.findViewById(R.id.reply_type);
            mReply_connten = (com.rockerhieu.emojicon.EmojiconTextView1)  view.findViewById(R.id.reply_connten);
            mReply_yw = (com.rockerhieu.emojicon.EmojiconTextView1) view. findViewById(R.id.reply_yw);
            mPull_text = (TextView)  view.findViewById(R.id.pull_text);
            mMess_nosee = (TextView)  view.findViewById(R.id.mess_nosee);
            mReply_btn = (TextView)  view.findViewById(R.id.reply_btn);
            details_layout = (RelativeLayout) view.findViewById(R.id.details_layout);
            isvip= (ImageView) view.findViewById(R.id.isvip);
        }
    }

}
